static char SccsId[] = "@(#)aslzrot.c	1.7   10/6/95";
/*
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */

#define	_ASLLIB_
#define	_ASLZROT_C_

#define	___ASL_CHECK_ANGLE 

#include <farl.h>
#include <asl.h>

#if !defined( max )
#define max(x,y)	(((x)>(y))?(x):(y))
#endif
#if !defined( min )
#define min(x,y)	(((x)<(y))?(x):(y))
#endif

#if !defined( sindi )
#if !defined( ___ASL_CHECK_ANGLE )
#define	sindi(x)	( (short)FarlGetSin187( (x) ) << 1 )
#else
#define	sindi(x)	( (short)FarlGetSin187(				\
					( ( (x) % 3600 ) >= 0 ) ?	\
					( (x) % 3600 ) :		\
					( ( (x) % 3600 ) + 3600 ) ) << 1 )
#endif
#endif

#if !defined( cosdi )
#if !defined( ___ASL_CHECK_ANGLE )
#define	cosdi(x)	( (short)FarlGetCos187( (x) ) << 1 )
#else
#define	cosdi(x)	( (short)FarlGetCos187(				\
					( ( (x) % 3600 ) >= 0 ) ?	\
					( (x) % 3600 ) :		\
					( ( (x) % 3600 ) + 3600 ) ) << 1 )
#endif
#endif

/*----------------------------------------------------------------------------*/
void
ASLMakeZoomRotateSprite( ASLSource *src,
	short dstX,		short dstY,
	short theta,		short scale,		ASLSct *sct )
{
	int	left, top, right, bottom;
	int	leftTopX, leftTopY, rightTopX, rightTopY;
	int	leftBottomX, leftBottomY, rightBottomX, rightBottomY;

	left   =  -( src->x_hot );
	right  =  src->x_size - src->x_hot;
	top    =  -( src->y_hot );
	bottom =  src->y_size - src->y_hot;

	leftTopX     = dstX * 65536 + ( left   * cosdi( theta )
					- top    * sindi( theta ) ) * scale;
	leftTopY     = dstY * 65536 + ( left   * sindi( theta )
					+ top    * cosdi( theta ) ) * scale;
	rightTopX    = dstX * 65536 + ( right  * cosdi( theta )
					- top    * sindi( theta ) ) * scale;
	rightTopY    = dstY * 65536 + ( right  * sindi( theta )
					+ top    * cosdi( theta ) ) * scale;
	leftBottomX  = dstX * 65536 + ( left   * cosdi( theta )
					- bottom * sindi( theta ) ) * scale;
	leftBottomY  = dstY * 65536 + ( left   * sindi( theta )
					+ bottom * cosdi( theta ) ) * scale;
	rightBottomX = dstX * 65536 + ( right  * cosdi( theta )
					- bottom * sindi( theta ) ) * scale;
	rightBottomY = dstY * 65536 + ( right  * sindi( theta )
					+ bottom * cosdi( theta ) ) * scale;

	left   = min( min( leftTopX, rightTopX ),
			min( leftBottomX, rightBottomX ) ) - 131071;
	right  = max( max( leftTopX, rightTopX ),
			max( leftBottomX, rightBottomX ) ) + 131071;
	top    = min( min( leftTopY, rightTopY ),
			min( leftBottomY, rightBottomY ) ) - 131071;
	bottom = max( max( leftTopY, rightTopY ),
			max( leftBottomY, rightBottomY ) ) + 131071;

	if ( ( left == right ) || ( top == bottom )
			|| ( left >= ( 320 * 65536 ) ) || ( right  < 0 )
			|| ( top  >= ( 256 * 65536 ) ) || ( bottom < 0 ) ) {
		sct->head	 = ASL_VAL_FNC_NONE;
		return;
	}

	if ( left   < (   0         ) ) left   =   0;
	if ( top    < (   0         ) ) top    =   0;
	if ( right  > ( 320 * 65536 ) ) right  = 320 * 65536;
	if ( bottom > ( 256 * 65536 ) ) bottom = 256 * 65536;
	if ( ( right - left ) > 256 * 65536 ) {
#if defined( __ASL_FORCE_DRAW )
		right = left + 256 * 65536;
#else
		sct->head	 = ASL_VAL_FNC_NONE;
		return;
#endif
	}
	leftTopX     = src->x_hot + ( 
		  ( left / 256 - dstX * 256 ) * cosdi( 3600 - theta )
		- ( top  / 256 - dstY * 256 ) * sindi( 3600 - theta ) )
							/ ( scale * 256 );
	leftTopY     = src->y_hot + (
		  ( left / 256 - dstX * 256 ) * sindi( 3600 - theta )
		+ ( top  / 256 - dstY * 256 ) * cosdi( 3600 - theta ) )
							/ ( scale * 256 );

	sct->head	 = ASL_VAL_FNC_ZOOMROT
					| ( src->bank & ASL_MSK_BANKSEL );
	sct->cntl	 = 0;
	sct->dsp_x_pos	 = ( left / 65536 ) & ASL_MSK_DSP_X_POS;
	sct->dsp_y_pos	 = ( top  / 65536 ) & ASL_MSK_DSP_Y_POS;
	sct->dsp_xy_size = ( ( ( ( right - left ) / 65536 - 1 )
				<< ASL_SFT_DSP_X_SIZE ) & ASL_MSK_DSP_X_SIZE )
			 | ( ( ( ( bottom - top ) / 65536 - 1 )
				<< ASL_SFT_DSP_Y_SIZE ) & ASL_MSK_DSP_Y_SIZE );
	sct->src_xy_pos	 =
		  ( (  src->x_pos << ASL_SFT_SRC_X_POS ) & ASL_MSK_SRC_X_POS )
		| ( (  src->y_pos << ASL_SFT_SRC_Y_POS ) & ASL_MSK_SRC_Y_POS );
	sct->src_xy_size = ( ( ( src->x_size - 1 ) << ASL_SFT_SRC_X_SIZE )
							& ASL_MSK_SRC_X_SIZE )
			 | ( ( ( src->y_size - 1 ) << ASL_SFT_SRC_Y_SIZE )
							& ASL_MSK_SRC_Y_SIZE );
	sct->src_x_off	 =  leftTopX;
	sct->src_y_off	 =  leftTopY;
	sct->src_x_scan	 =  ( cosdi( theta ) * 256 / (int)scale );
	sct->src_y_scan	 = -( sindi( theta ) * 256 / (int)scale );
}

/*----------------------------------------------------------------------------*/
